package j2;
import java.io.*;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;

public class jrun {
        public static void main(String[] args) {
            a();
        }
        public static void a(){
            try (Scanner sc = new Scanner(System.in); BufferedReader reader = new BufferedReader(new FileReader("src/main/java/j2/data.txt"))) {
                Random random = new Random();
                Pattern pattern = Pattern.compile("^\\s*\\d{1,3}\\s*[-+]\\s*\\d{1,3}\\s*=\\s*-?\\d{1,3}\\s*$");
                String l;
                int i=0;
                String[] e = new String[1000];
                while ((l = reader.readLine()) != null && i < e.length) {
                    if (pattern.matcher(l).matches()) {
                        e[i] = l;
                        i++;
                    }
                }
                System.out.println("错题题库如下：");
                for (int j = 0; j < e.length; j++) {
                    if (e[j] != null) {
                        System.out.printf("(%3d)" + e[j].substring(0, 9) + "  ", j + 1);
                        if (j % 5 == 4) {
                            System.out.println();
                            System.out.println("-------------");
                        }
                    }
                }
                System.out.println();
                System.out.println("做题(0)/管理题库(1)/退出：");
                String x0 = sc.next();
                if (x0.equals("1")) {
                    jsub.g3(e, i);
                } else if(x0.equals("0")){
                    int x = i + 1;
                    String[] f = new String[i];
                    String[] g = new String[i];
                    while (x > i) {
                        System.out.println("请说出需随机抽出多少题练习：");
                        x = sc.nextInt();
                        if (x > i) {
                            System.out.println("习题太多了请重新输入：");
                        } else {
                            for (int j = 0; j < x; j++) {
                                int x1 = random.nextInt(i);
                                f[j] = e[x1];
                                g[j] = e[x1].substring(12);
                                for (int k = 0; k < j; k++) {
                                    if (f[j].equals(f[k])) {
                                        f[j] = null;
                                        j--;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    System.out.println("习题：");
                    System.out.println("-------------");
                    double h = (double) 1 / x;
                    double h1 = 0;
                    for (int j = 0; j < x; j++) {
                        System.out.println(f[j].substring(0, 12) + "???");
                        String x2 = String.format("%3s", sc.next());
                        if (x2.equals("uno")) {
                            break;
                        } else if (x2.equals(g[j])) {
                            System.out.println("恭喜你答对了！");
                            h1 += h;
                        } else {
                            System.out.println("答错了,正确答案是" + g[j]);
                        }
                    }
                    System.out.println("-------------");
                    System.out.printf("本次练习的正确率为%.2f%%", h1 * 100);
                }
                else {
                    System.out.println("已退出");
                };
            } catch (FileNotFoundException fnfe) {
                System.out.println("文件未找到: " + fnfe.getMessage());
            } catch (IOException ioe) {
                System.out.println("读取文件时出错: " + ioe.getMessage());
            }
        }
    }